# project name
PROJECT = moonlander

# base directories
SDKROOT = /c/devkitPro
DEVKITDIR = $(SDKROOT)/devkitARM

# ARM SDK
INCLUDE = $(DEVKITDIR)/arm-elf/include
LIBDIR = $(DEVKITDIR)/arm-elf/lib/interwork

# GBA SDK
INCLUDE2 = $(SDKROOT)/include
LIBDIR2 = $(SDKROOT)/libgba/lib

# multiboot compliant (BaseAdress 0x02000000)
SPECS = gba_mb.specs

# cartridge compliant (BaseAdress 0x08000000)
#SPECS = gba.specs


# TOOLS and Options
CC = $(DEVKITDIR)/bin/arm-eabi-gcc.exe
CFLAGS = -I. -I $(INCLUDE2) -I $(INCLUDE) -c -g -O2 -Wall -mcpu=arm7tdmi -mtune=arm7tdmi -fomit-frame-pointer -ffast-math -mthumb -mthumb-interwork
LDFLAGS =  -mthumb -mthumb-interwork -specs $(SPECS) -L $(LIBDIR) -L $(LIBDIR2)

OC = $(DEVKITDIR)/bin/arm-eabi-objcopy.exe
GBAFIX = $(DEVKITDIR)/bin/gbafix
GB = $(SDKROOT)/vba_emu/VisualBoyAdvance.exe

# files
OBJ = $(PROJECT).o
BIN = $(PROJECT)


# the rules
$(BIN): $(OBJ) 
		$(CC) -o $(PROJECT).elf $(OBJ) $(LDFLAGS)
		$(OC) -v -O binary $(PROJECT).elf $(PROJECT).gba
		$(GBAFIX) $(PROJECT).gba

%.o: %.s
		$(CC) -c $<

%.s: %.c
		$(CC) $(CFLAGS) -S $<

.PHONY: clean
clean: 
		rm -rf $(BIN) $(BIN).elf $(BIN).gba $(OBJ) *~

run: $(BIN)
		$(GB) $(PROJECT).gba &
